package process
import (
	"fmt"
	"go_code/projectExec2/common/message"
	"go_code/projectExec2/server/utils"
	"net"
	"encoding/json"
)

type SmsProcess struct{

}

//转发消息
func (this *SmsProcess)SendGroupMes(mes *message.Message){		
	//将消息取出
	var smsmes message.SmsMes
	err := json.Unmarshal([]byte(mes.Data),&smsmes)
	if err != nil{
		fmt.Println("json.Unmarshal err :",err)
		return 
	}
	data,err := json.Marshal(mes)
	if err != nil{
		fmt.Println("json.Marshal err :",err)
		return 
	}
	//遍历服务器端的onlineUsers map[int]*UserProcess
	for id,up := range userMgr.onlineUsers{
		if id == smsmes.UserId{
			continue
		}
		this.SendMesToEachOnlineUser(data,up.Conn)
	}
}

func (this *SmsProcess)SendMesToEachOnlineUser(data []byte,conn net.Conn){
	//创建一个Transfer实例，发送data
	tf := &utils.Transfer{
		Conn : conn,
	}
	err := tf.WritePkg(data)
	if err != nil{
		fmt.Println("转发消息失败~ err: ",err)
	}
}